from pynput import keyboard
from pynput import mouse
import time


effect_insert_co = 0


skip = False

is_pressed0 = False
is_pressed = False
is_pressed2 = False
press_co = 0
press_state = False
insert_co = 0
is_working = True

alt_pressed = False
altr_pressed = False
shift_pressed = False
ctrl_pressed = False
ctrlr_pressed = False

left_pressed = False

def on_press(key):
    global is_pressed, is_pressed2, is_pressed0, insert_co, alt_pressed, shift_pressed, ctrl_pressed, ctrlr_pressed
    global left_pressed
    
    if 'alt' in str(key):
        alt_pressed = True
    
    if 'shift' in str(key):
        shift_pressed = True
        
    if 'ctrl' in str(key):
        if 'ctrl_r' in str(key):
            ctrlr_pressed = True
        else:
            ctrl_pressed = True
        
    if 'insert' in str(key) and not shift_pressed and not ctrl_pressed:
        is_pressed0 = True
        insert_co += 1
        if insert_co == 10:
            insert_co = 0
    
    if not is_pressed0:
        return
    
    if 'ctrl_l' in str(key):
        is_pressed = True
        return
        
    if 'ctrl_r' in str(key):
        is_pressed2 = True
        return
        
def on_release(key):
    global is_pressed, skip, is_pressed2, is_pressed0, alt_pressed, is_working, press_co, insert_co
    global press_state, shift_pressed, ctrl_pressed, left_pressed, ctrlr_pressed
    
    if 'ctrl' in str(key):
        if 'ctrl_r' in str(key):
            ctrlr_pressed = False
        else:
            ctrl_pressed = False
        
    if 'shift' in str(key):
        shift_pressed = False
        
    if 'alt' in str(key):
        alt_pressed = False
    
    if left_pressed and  'ctrl_r' in str(key):
        mouse.Controller().release(mouse.Button.left)
        left_pressed = False
        return
        
    if not 'insert' in str(key):
        return    
    
    if alt_pressed:
        is_working = not is_working
        if is_working:
            skip = False            
            is_pressed0 = False
            is_pressed = False
            is_pressed2 = False
            press_co = 0
            press_state = False
            insert_co = 0
        print('is_working', is_working)
        return
    
    if not is_working:
        return
    
    is_pressed0 = False
    if skip:
        skip = False
        return    
    if insert_co % 2 != 0:        
        skip = True        
        keyboard.Controller().press(key)
        keyboard.Controller().release(key)
        
    
    if is_pressed2:
        is_pressed2 = False
        if not press_state:
            mouse.Controller().press(mouse.Button.left)
            print(1)
            press_state = True
        else:
            mouse.Controller().release(mouse.Button.left)
            press_state = False
            print(2)
        is_pressed = False
        return
    print(3)
    press_state = False
    if not is_pressed:
        mouse.Controller().click(mouse.Button.left,1)        
    else: 
        mouse.Controller().click(mouse.Button.right,1)
    is_pressed = False
   
listener = keyboard.Listener(
    on_release=on_release,
    on_press=on_press,
    
)
listener.start()
import time
while True:
    time.sleep(100)